var connect = require('connect');

connect.createServer(
    connect.static(__dirname)
).listen(1337);

var ws = require('websocket.io');

var server = ws.listen(8888, function() {
    console.log('Start WebSocket Server!');
});

server.on('connection', function(socket) {
    socket.on('message', function(data) {
        console.log('Receive: ' + data);
        
        // モータ制御処理呼び出し関数。
        motorExec(data);
    });
});

var motorExec = function(data) {   
    // 引数を配列に格納。
    var args = data.split(" ");
    
    var spawn = require('child_process').spawn;
    
    var motorDrive = spawn("./DriveMecanum", args);
//    var motorDrive = spawn("./CommandArgs", args);
    
    motorDrive.stdout.on('data', function(data) {
        console.log('stdout: ' + data);
    });
    
    console.log('Motor Drived.');
}
